package service;

import util.Globals.Field;

public class Filter {
	private String[] direction;
	private Field[] field;
	public String[] getDirection() {
		return direction;
	}
	public void setDirection(String[] direction) {
		this.direction = direction;
	}
	public Field[] getField() {
		return field;
	}
	public void setField(Field[] field) {
		this.field = field;
	}
	
	public Filter(String in) throws Exception {
		String[] filters = in.split(",");
		direction = new String[filters.length];
		field = new Field[filters.length];
		int i = 0;
		for(String s : filters){
			String[] help = s.split("\\.");
			if(!help[0].equals("filter"))throw new Exception("This is not a filter! "+in);
			direction[i] = help[2];
			if(help[1].equals("name")) field[i] = Field.NAME;
			if(help[1].equals("edition")) field[i] = Field.EDITION;
			if(help[1].equals("buy")) field[i] = Field.BUY;
			if(help[1].equals("sell")) field[i] = Field.SELL;
			
		}
		
	}
	public boolean isHibernateCompatible(){
		boolean result  = true;
		for(Field f : field){
			if(f == Field.BUY || f == Field.SELL){
				result = false;
				break;
			}
		}
		return result;
	}
	public int getSize(){
		return direction.length;
	}
	
}
